<%= render partial: 'spree/admin/shared/sub_menu/configuration' %>

<% content_for :table_filter_title do %>
  <%= Spree.t(:search) %>
<% end %>

<% content_for :table_filter do %>
    <div data-hook="admin_store_credits_index_search">
      <%= search_form_for [:admin, @search] do |f| %>
        <div class="alpha nine columns">
          <div class="field">
            <%= f.label :transaction_id_eq, Spree.t(:transaction_id) %>
            <%= f.text_field :transaction_id_eq %>
          </div>

          <div class="field">
            <%= f.label :transactioner_email_cont, Spree.t(:transactioner_email) %>
            <%= f.email_field :transactioner_email_cont %>
          </div>

          <% unless @user %>
            <div class="field">
              <%= f.label :user_email_cont, Spree.t(:user_email) %>
              <%= f.email_field :user_email_cont %>
            </div>
          <% end %>
        </div>

      <div class="clear"></div>

      <div class="form-buttons actions filter-actions" data-hook="admin_products_index_search_buttons">
        <%= button Spree.t(:search), 'icon-search' %>
      </div>
      <% end %>
    </div>
<% end %>

<% content_for :page_title do %>
  <% if @user %>
    <%= Spree.t(:store_credits_for_user_email, user_email: @user.email) %> (<%= Spree.t(:current_balance_for_user, total: @user.store_credits_total) %>)
  <% else %>
    <%= Spree.t(:listing_store_credits) %>
  <% end %>
<% end %>

<% if @user %>
  <% content_for :page_actions do %>
    <%= button_link_to Spree.t(:new_credit), new_admin_user_credit_path(user_id: @user), class: "btn-success", icon: 'add' %>
    <%= button_link_to Spree.t(:new_debit), new_admin_user_debit_path(user_id: @user), class: "btn-success", icon: 'add' %>
  <% end %>
<% end %>

<%= paginate @store_credits %>

<table class="table index" id="transaction_listing" data-hook>
  <thead>
    <tr data-hook="transactions_index_headers">
      <th><%= sort_link @search, :transaction_id, Spree.t(:transaction_number) %></th>
      <% unless @user %>
        <th class="actions"><%= sort_link @search, :user_email, Spree.t(:user) %></th>
      <% end %>
      <th class="actions"><%= sort_link @search, :transactioner_email, Spree.t(:transactioner_email) %></th>
      <th class="actions"><%= sort_link @search, :amount, Spree.t(:amount) %></th>
      <th class="actions"><%= sort_link @search, :balance, Spree.t(:balance) %></th>
      <% if @user %>
        <th class="actions"><%= sort_link @search, :reason, Spree.t(:reason) %></th>
        <th class="actions"><%= sort_link @search, :payment_mode, Spree.t(:payment_mode) %></th>
      <% end %>
      <th class="actions"><%= sort_link @search, :type, Spree.t(:type) %></th>
      <% unless @user %>
        <th data-hook="admin_users_index_header_actions" class="actions"><%= Spree.t(:action) %></th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% @store_credits.each do |store_credit|%>
      <tr id="<%= cycle('odd', 'even') %>" data-hook="transactions_index_rows">
        <td class="align-center"><%= store_credit.transaction_id %></td>
        <% unless @user %>
          <td class="align-center"><%= link_to store_credit.user.email, admin_user_path(store_credit.user) %></td>
        <% end %>
        <td class="align-center"><%= store_credit.transactioner.try(:email) %></td>
        <td class="align-center"><%= number_to_currency store_credit.amount %></td>
        <td class="align-center"><%= number_to_currency store_credit.balance %></td>
        <% if @user %>
          <td class="align-center"><%= store_credit.reason %></td>
          <td class="align-center"><%= store_credit.class::PAYMENT_MODE.key(store_credit.payment_mode) %>
        <% end %>
        <td class="align-center"><%= store_credit.type.demodulize %></td>
        <% unless @user %>
          <td><%= link_to Spree.t(:credit), new_admin_user_credit_path(user_id: store_credit.user) %> | <%= link_to Spree.t(:debit), new_admin_user_debit_path(user_id: store_credit.user) %> | <%= link_to Spree.t(:view), admin_user_store_credits_path(user_id: store_credit.user) %></td>
        <% end %>
      </tr>
      <% end %>
  </tbody>
</table>
